package com.dq.health.config;

import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.HashSet;
import java.util.Set;

@EnableSwagger2
@EnableWebMvc
@Profile({"dev", "test"})
public class SwaggerConfig {

    @Value("${user.swaggerEnabled:false}")
    private boolean swaggerEnabled;

    @Bean
    public Docket customDocket() {
        Set<String> set = new HashSet<>();
        set.add("application/json");
        return new Docket(DocumentationType.SWAGGER_2).enable(swaggerEnabled).produces(set).apiInfo(apiInfo()).select()
            .apis(RequestHandlerSelectors.any()).paths(Predicates.not(PathSelectors.regex("/error.*"))).build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("社区医疗", "http://localhost/", "");
        return new ApiInfoBuilder().title("社区医疗").description("API接口").contact(contact).version("1.0.0").build();
    }
}
